<template>
  <el-dialog
    :title="dialogType ==='dialog_add'? text_add : text_update"
    :close-on-click-modal="isDebug"
    :visible.sync="dialogVisible">
    <!--:rules="dataRule"-->
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm"  label-width="130px" label-position="left" >
    <el-form-item label="头像x" prop="image">
      <el-input v-model="dataForm.image" placeholder="头像"></el-input>
    </el-form-item>
    <el-form-item label="名字" prop="name">
      <el-input v-model="dataForm.name" placeholder="名字"></el-input>
    </el-form-item>
    <el-form-item label="点赞(跟单)数量" prop="likeCount">
      <el-input v-model="dataForm.likeCount" placeholder="点赞(跟单)数量"></el-input>
    </el-form-item>
    <el-form-item label="老师简介" prop="content">
      <el-input v-model="dataForm.content" placeholder="老师简介"></el-input>
    </el-form-item>
    <el-form-item label="喊单成功率" prop="hdSuccessRate">
      <el-input v-model="dataForm.hdSuccessRate" placeholder="喊单成功率"></el-input>
    </el-form-item>
    <el-form-item label="盈利率" prop="profitRate">
      <el-input v-model="dataForm.profitRate" placeholder="盈利率"></el-input>
    </el-form-item>
    <el-form-item label="长江银/现货银/白金铜" prop="productName">
      <el-input v-model="dataForm.productName" placeholder="长江银/现货银/白金铜"></el-input>
    </el-form-item>
    <el-form-item label="仓位数" prop="cwCount">
      <el-input v-model="dataForm.cwCount" placeholder="仓位数"></el-input>
    </el-form-item>
    <el-form-item label="点位数" prop="dwCount">
      <el-input v-model="dataForm.dwCount" placeholder="点位数"></el-input>
    </el-form-item>
    <el-form-item label="收益" prop="profit">
      <el-input v-model="dataForm.profit" placeholder="收益"></el-input>
    </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="dataForm.createTime"
          type="datetime"
          value-format="timestamp"
          placeholder="选择时间">
        </el-date-picker>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取消</el-button>
      <el-button type="primary" @click="dialogType ==='dialog_add'?requestAdd():requestUpdate()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        dialogVisible: false,
        tempDataFormForm: null,
        dialogType: 'dialog_add', // 弹窗类型 1添加 2编辑
        dataForm: {
          id: 0,
          image: '',
          name: '',
          likeCount: '',
          content: '',
          hdSuccessRate: '',
          profitRate: '',
          productName: '',
          cwCount: '',
          dwCount: '',
          profit: '',
          createTime: '',
          updateTime: ''
        },
        dataRule: {
          image: [{ required: true, message: '头像不能为空', trigger: 'blur' }],
          name: [{ required: true, message: '名字不能为空', trigger: 'blur' }],
          likeCount: [{ required: true, message: '点赞(跟单)数量不能为空', trigger: 'blur' }],
          content: [{ required: true, message: '老师简介不能为空', trigger: 'blur' }],
          hdSuccessRate: [{ required: true, message: '喊单成功率不能为空', trigger: 'blur' }],
          // profitRate: [{ required: true, message: '盈利率不能为空', trigger: 'blur' }],
          profitRate: [{validator: this.checkDecimal, trigger: 'change'}],
          productName: [{ required: true, message: '长江银/现货银/白金铜不能为空', trigger: 'blur' }],
          cwCount: [{ required: true, message: '仓位数不能为空', trigger: 'blur' }],
          dwCount: [{ required: true, message: '点位数不能为空', trigger: 'blur' }],
          profit: [{ required: true, message: '收益不能为空', trigger: 'blur' }],
          createTime: [{ required: true, message: '创建时间不能为空', trigger: 'blur' }],
          updateTime: [{ required: true, message: '修改时间不能为空', trigger: 'blur' }]
        }
      }
    },
    props: {
      rowData: {
        type: Object,
        default: null
      }
    },
    methods: {
      initDialog (dialogType) {
        this.dialogVisible = true
        this.dialogType = dialogType
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (dialogType === 'dialog_update') {
            this.requestInfo()
          }
        })
      },
      /** 请求信息(表单回显) */
      requestInfo () {
        this.$httpRequest({
          // url: `/api/teacher/info/${this.rowData.id}`,
          url: `/api/teacher/info/` + this.rowData.id,
          method: 'get'
        }).then((data) => {
          this.dataForm.image = data.image
          this.dataForm.name = data.name
          this.dataForm.likeCount = data.likeCount
          this.dataForm.content = data.content
          this.dataForm.hdSuccessRate = data.hdSuccessRate
          this.dataForm.profitRate = data.profitRate
          this.dataForm.productName = data.productName
          this.dataForm.cwCount = data.cwCount
          this.dataForm.dwCount = data.dwCount
          this.dataForm.profit = data.profit
          this.dataForm.createTime = data.createTime
          this.dataForm.updateTime = data.updateTime
        }, error => {
          this.showErrorToast(error.msg)
        })
      },
      /** 请求添加 */
      requestAdd () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$httpRequest({
              url: `/api/teacher/add`,
              method: 'post',
              data: {
                'id': this.dataForm.id || undefined,
                'image': this.dataForm.image,
                'name': this.dataForm.name,
                'likeCount': this.dataForm.likeCount,
                'content': this.dataForm.content,
                'hdSuccessRate': this.dataForm.hdSuccessRate,
                'profitRate': this.dataForm.profitRate,
                'productName': this.dataForm.productName,
                'cwCount': this.dataForm.cwCount,
                'dwCount': this.dataForm.dwCount,
                'profit': this.dataForm.profit,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime
              }
            }).then(() => {
              this.dialogVisible = false
              this.showSuccessToast(this.toast_text_add_success, () => {
                this.$emit('refreshDataList')
              })
            }, error => {
              this.showErrorToast(error.msg)
            })
          }
        })
      },
      /** 请求更新 */
      requestUpdate () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$httpRequest({
              url: `/api/teacher/update`,
              method: 'post',
              data: {
                'id': this.dataForm.id || undefined,
                'image': this.dataForm.image,
                'name': this.dataForm.name,
                'likeCount': this.dataForm.likeCount,
                'content': this.dataForm.content,
                'hdSuccessRate': this.dataForm.hdSuccessRate,
                'profitRate': this.dataForm.profitRate,
                'productName': this.dataForm.productName,
                'cwCount': this.dataForm.cwCount,
                'dwCount': this.dataForm.dwCount,
                'profit': this.dataForm.profit,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime
              }
            }).then(() => {
              this.dialogVisible = false
              this.showSuccessToast(this.toast_text_update_success, () => {
                this.$emit('refreshDataList')
              })
            }, error => {
              this.showErrorToast(error.msg)
            })
          }
        })
      }
    }
  }
</script>
